/**
 * Beaver - Zend/Ext.Direct framework
 *
 * @license     http://www.gnu.org/licenses/gpl-2.0.txt
 * @author      Mihály Kuprivecz <mkuprivecz@gmail.com>
 * @namespace   Beaver.Modules.User
 */

Ext.ns('Beaver.Modules.User');

Beaver.Modules.User.data = {};

Beaver.Modules.User.UI = {};

Beaver.Modules.User.UI.loginFormPanel = new Ext.FormPanel({
	labelWidth:  95,
	frame:       true, 
	title:       '<?php echo $this->beaverLanguage->get("User","login"); ?>', 
	defaultType: 'textfield',
	labelAlign:  'right',
	waitMsgTarget : Beaver.Modules.User.UI.loginWindow,
	items:[
		{ 
				fieldLabel: '<?php echo $this->beaverLanguage->get("User","username"); ?>',
				msgTarget:  'side',
				emptyText:  '<?php echo $this->beaverLanguage->get("User","username"); ?>',
				name:       'username', 
				allowBlank: false
		},
		{ 
				fieldLabel: '<?php echo $this->beaverLanguage->get("User","password"); ?>',
				msgTarget:  'side',
				emptyText:  '<?php echo $this->beaverLanguage->get("User","password"); ?>',
				name:       'password', 
				inputType:  'password', 
				allowBlank: false
		}
	],
	buttons:[{
		text:    'Login',
		handler: function()
		{
			Beaver.Modules.User.UI.loginFormPanel.getForm().submit({
				success : function(event,result){
					Beaver.Modules.User.UI.loginFormPanel.getForm().setValues({ 'password' : '' });
					Beaver.Modules.User.data = result.result.data;
					Beaver.Modules.User.onLoginSuccess();
				},
				failure : Beaver.Modules.User.onLoginFailure,
				waitMsg: '<?php echo $this->beaverLanguage->get("User","wait"); ?>...'
			});
		}
	}],
	api: {
        submit: Beaver.Remote.User.login
	}
});
 
Beaver.Modules.User.UI.loginWindow = new Ext.Window({
		layout:    'fit',
		width:     300,
		modal:     true,
		height:    150,
		closable:  false,
		resizable: false,
		plain:     true,
		border:    false,
		items:     Beaver.Modules.User.UI.loginFormPanel
});

Beaver.Modules.User.showLoginScreen = function()
{
	Beaver.Modules.User.UI.loginWindow.show();
};

Beaver.Modules.User.hideLoginScreen = function()
{
	Beaver.Modules.User.UI.loginWindow.hide();
}

Beaver.Modules.User.onLoginSuccess = function(){}

Beaver.Modules.User.onLoginFailure = function()
{
	Ext.Msg.alert('<?php echo $this->beaverLanguage->get("User","login_failed"); ?>');
}

Beaver.Modules.User.checkSession = function()
{
	Beaver.Remote.User.checksession();
};

/**
 * Store the session lifetime, for the session checker
 */
Beaver.Modules.User.sessionLifetime = <?php echo $this->lifetime; ?>;


Beaver.Modules.User.Store = Ext.extend( Beaver.Modules.General.Store, {
	constructor: function(config) {
		config        = config || {};
		config.fields = config.fields || [];
		config.controllerName = 'User';
		Ext.applyIf( config.fields, [
			{ name: 'id', mapping: 'id' },
			{ name: 'username', mapping: 'username', allowBlank: false },
			{ name: 'password', mapping: 'password', allowBlank: false },
			{ name: 'name', mapping: 'name', allowBlank: false },
			{ name: 'group', mapping: 'group', allowBlank: false },
			{ name: 'lastlogin', mapping: 'lastlogin' },
			{ name: 'modified', mapping: 'modified' },
			{ name: 'created', mapping: 'created' }
		]);
		
		Beaver.Modules.User.Store.superclass.constructor.apply(this, [config]);
	}
});

Ext.reg('beaveruser', Beaver.Modules.User.Store);


Beaver.Modules.User.Grid = Ext.extend(Beaver.Modules.General.Grid, {
	initComponent: function() {
		var store  = new Beaver.Modules.User.Store();
		var editor = new Beaver.Modules.General.RowEditor();
		editor.on('canceledit', function() {
			if(store.getAt(0).phantom)
			{
				store.removeAt(0);
			}
		});
		var typeStore = new Ext.data.ArrayStore({
							id: 0,
							fields: [ 'id', 'type' ],
							data : [[1,'admin'],[2,'user']]
						});
		var self  = this;
		var config = {
			store: store,
			plugins: [ editor ],
			columns: [
				{
					header: 'id',
					width: 20,
					sortable: true,
					dataIndex: 'id'
				},
				{
					header: 'Név',
					sortable: true,
					dataIndex: 'name',
					editor: new Ext.form.TextField()
				},
				{
					header: 'Felhasználói név',
					sortable: true,
					dataIndex: 'username',
					editor: new Ext.form.TextField()
				},
				{
					header: 'Jelszó',
					sortable: true,
					dataIndex: 'password',
					editor: new Ext.form.TextField()
				},
				{
					header: 'Csoport',
					sortable: true,
					dataIndex: 'group',
					editor: new Ext.form.ComboBox({
						typeAhead: true,
						autoDestroy: true,
						triggerAction: 'all',
						lazyRender:true,
						mode: 'local',
						store : typeStore,
						valueField: 'id',
						displayField: 'type'
					}),
					renderer: function(value, metaData, record, rowIndex, colIndex, store) {
						return typeStore.getAt(typeStore.find("id",value)).data.type;
					},
				},
				{
					header: 'Utolsó belépés',
					sortable: true,
					dataIndex: 'lastlogin'
				},
				{
					header: 'Utolsó módosítás',
					sortable: true,
					dataIndex: 'modified'
				},
				{
					header: 'Létrehozva',
					sortable: true,
					dataIndex: 'created'
				}
			],
			tbar: [
				{
					text: 'Felhaszáló hozzáadása',
					handler: function(){

						if(self.store.getAt(0) && self.store.getAt(0).phantom)
						{
							self.store.removeAt(0);
						}
						var record = new store.recordType({
							name : '',
							username : '',
							password : '',
							group : '2',
							lastlogin: '0000-00-00 00:00:00'
						});

						editor.stopEditing();
						self.store.insert(0, record);
						editor.startEditing(0);
					}
				},
				{
					text: 'Kijelöltek törlése',
					handler: function(){
						
						editor.stopEditing();
						var records = self.getSelectionModel().getSelections();
						if (records.length > 0)
						{
					
							Ext.Msg.confirm('Törélés', 'Biztosan törölni szeretné az összes kijelölt elemet?', function( answer ){
								if (answer == "yes")
								{
									self.getStore().remove(records);
								}
				        	},this);
						}
					}
				}
			]
		};
		Ext.apply(this, config);
		Ext.apply(this.initialConfig, config);
		Beaver.Modules.User.Grid.superclass.initComponent.apply(this, arguments);
	},
	onRender:function() {
		Beaver.Modules.User.Grid.superclass.onRender.apply(this, arguments);
	}
});